package com.dhcc.DRGsHosp.application.impl;

import java.util.List;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.JdbcTemplateObj;
import org.dayatang.querychannel.QueryChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.dhcc.DRGsHosp.application.LoginInfoApplication;
import com.dhcc.DRGsHosp.core.domain.LoginLog;
import com.dhcc.DRGsHosp.infra.util.ConstantEnum;


@Named
@Transactional
public class LoginInfoApplicationImpl implements LoginInfoApplication {

	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }
	@Autowired
	private JdbcTemplateObj jdbcTemplateObj;
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> getLoginInfo(String fromDate, String toDate) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select a.login_user_code, \n ");
		sql.append(" a.login_count, \n ");
		sql.append(" to_char(b.new_login_time,'yyyy-mm-dd hh24:mi:ss'), \n ");
		sql.append(" c.login_ip, \n ");
		sql.append(" u2.dep_code, \n ");
		sql.append(" d.dep_name,a.login_user_name \n ");
		sql.append(" from (select login_user_code,login_user_name, count(*) as login_count \n ");
		sql.append(" from DRGs.t_login_log l \n ");
		sql.append(" left join DRGS_HOSP_KOALA.KS_ACTORS u \n ");
		sql.append(" on l.login_user_code = u.user_account \n ");
		sql.append(" where 1 = 1 \n ");
		sql.append(" and to_char(l.login_time, 'yyyy-mm-dd') between '"+fromDate+"' and '"+toDate+"' \n ");
		sql.append(" group by login_user_code,login_user_name \n ");
		sql.append(" ) a \n ");
		sql.append(" left join ( \n ");
		sql.append(" select login_user_code, \n ");
		sql.append(" max(login_time) as new_login_time \n ");
		sql.append(" from DRGs.t_login_log \n ");
		sql.append(" group by login_user_code) b \n ");
		sql.append(" on a.login_user_code = b.login_user_code \n ");
		sql.append(" left join DRGs.t_login_log c \n ");
		sql.append(" on a.login_user_code = c.login_user_code and b.new_login_time=c.login_time \n ");
		sql.append(" left join DRGS_HOSP_KOALA.KS_ACTORS u2 \n ");
		sql.append(" on a.login_user_code = u2.user_account \n ");
		sql.append(" left join DRGs.t_department d \n ");
		sql.append(" on u2.dep_code = d.dep_code \n ");
		sql.append(" order by b.new_login_time desc \n ");
		List<Object[]> loginInfoList =  getQueryChannelService().createSqlQuery(sql.toString()).list();
		return loginInfoList;
	}

	/**
	 *  @Description：获取当前登录最大值
	 *  @Author：liuhongjun 
	 *  @see com.dhcc.DRGsHosp.application.LoginInfoApplication#getLastestIssue()
	 */
	@Override
	public String getLastestIssue() {
		return jdbcTemplateObj.queryForObject("SELECT to_char(max(t.login_time),'yyyy-mm-dd') logdate FROM DRGS.t_Login_Log t", String.class);
	}

	@Override
	public void creatLoginInfo(LoginLog loginLog) {
		loginLog.save();		
	}

	@Override
	public List<Object[]> getLoginHistory(String userCode, String dateFrom,
			String dateTo) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select a.login_user_code,  \n ");
		sql.append(" a.login_user_name, \n ");
		sql.append(" a.login_ip, \n ");
		sql.append(" to_char(a.login_time,'yyyy-mm-dd hh24:mi:ss') \n ");
		sql.append(" from drgs.t_login_log a \n ");
		sql.append(" where to_char(a.login_time, 'yyyy-mm-dd') between '"+dateFrom+"' and '"+dateTo+"' \n ");
		sql.append(" and a.login_user_code='"+userCode+"' \n ");
		sql.append(" order by a.login_time desc \n ");
		List<Object[]> resultList = getQueryChannelService().createSqlQuery(sql.toString()).list();
		return resultList;
	}
}
